﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class StoreEntry : System.Web.UI.Page
    {
        private StoreManager aManager = new StoreManager();
        private Store aStore = new Store();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGridview();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveStore(object sender, EventArgs e)
        {
            try
            {
                aManager = new StoreManager();
                aStore.StoreName = storeNameTextBox.Value;
                string message = aManager.SaveStore(aStore);
                messageResult.InnerText = message;
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Generic name can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            storeIdTextBox.Value = "";
            storeNameTextBox.Value = "";
        }
        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int StoreId = Convert.ToInt32(storeIdTextBox.Value);
                StoreManager storeManager = new StoreManager();
                string aa = storeManager.DeleteStore(StoreId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        private void LoadGridview()
        {
            try
            {
                List<Store> astores;
                astores = aManager.GetAllStore();
                storeGridView.DataSource = astores;
                storeGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridViewOnSaerch(int i, string storeIdorName)
        {
            try
            {
                storeGridView.DataSource = null;
                storeGridView.DataBind();
                List<Store> stores;
                StoreManager storeManager = new StoreManager();
                stores = storeManager.GetAllStoreByIdOrName(i, storeIdorName);
                storeGridView.DataSource = stores;
                storeGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSaerch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSaerch(i, searchNameTextBox.Value);
        }
        protected void storeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            storeGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }


    }
}